//
//  ServersViewController.m
//  jboss-admin
//
//  Created by Christos Vasilakis on 19/06/2010.
//  Copyright 2010 All rights reserved.
//

#import "ServersViewController.h"
#import "ServerDetailController.h"
#import "ServerMainController.h"
#import "JBossServer.h"

@implementation ServersViewController

@synthesize list;

- (void)viewDidLoad {
	[super viewDidLoad];
	
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);	
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Servers.archive"];
	
	NSData *data;
	NSKeyedUnarchiver *unarchiver;
	
	data = [[NSData alloc] initWithContentsOfFile:path];
	unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
	
	NSMutableArray *array = [unarchiver decodeObjectForKey:@"Servers"];
	self.list = array;
	
	[unarchiver finishDecoding];
	[unarchiver release];
	[data release];
	
	self.title = @"Servers";
	
	UIBarButtonItem *editButton = self.editButtonItem; 
    [editButton setTarget:self];
    [editButton setAction:@selector(toggleEdit)];
    self.navigationItem.leftBarButtonItem = editButton;
    
	UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addServer)];
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];

	NSLog(@"ServersViewController viewDidLoad");
}

- (void)viewWillAppear:(BOOL)animated {
	[self.tableView reloadData];
}

- (void)viewDidUnload {
	[super viewDidUnload];
	
	self.list = nil;
	
	NSLog(@"ServersViewController viewDidUnload");
}

- (void)dealloc {
	[list release];

	NSLog(@"ServersViewController deAlloc");
	
	[super dealloc];
}

#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [self.list count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *ServerCellIdentifier = @"Server";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ServerCellIdentifier];
	
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ServerCellIdentifier] autorelease];
	}
	
	NSUInteger row = [indexPath row];
	
	JBossServer *server = [self.list objectAtIndex:row];
	
	cell.textLabel.text = server.name;
	cell.detailTextLabel.text = server.hostname;
	cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

	return cell;
}

#pragma mark -
#pragma mark Table Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	NSUInteger row = [indexPath row];
	
	JBossServer *server = [self.list objectAtIndex:row];
	
	ServerMainController *mainController = [[ServerMainController alloc] initWithStyle:UITableViewStylePlain];
	mainController.server = server;
	
	[self.navigationController pushViewController:mainController animated:YES];
	[mainController release];
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
	NSUInteger row = [indexPath row];
	
	JBossServer *server = [self.list objectAtIndex:row];
	
	ServerDetailController *detailController = [[ServerDetailController alloc] initWithStyle:UITableViewStyleGrouped];
	detailController.title = server.name;
	detailController.server = server;

	[self.navigationController pushViewController:detailController animated:YES];
	[detailController release];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
	if (editingStyle == UITableViewCellEditingStyleDelete) {
		NSUInteger row = [indexPath row];
		NSArray *paths = [NSArray arrayWithObject: [NSIndexPath indexPathForRow:row inSection:0]];
	
		[self.list removeObjectAtIndex:row];
		[[self tableView] deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
	}
}

#pragma mark -
#pragma mark Action Calls
- (IBAction)addServer {
	JBossServer *server = [[JBossServer alloc] init];
	
	ServerDetailController *detailController = [[ServerDetailController alloc] initWithStyle:UITableViewStyleGrouped];
	detailController.title = @"New Server";
	detailController.server = server;
	detailController.list = list;
	
	[self.navigationController pushViewController:detailController animated:YES];
	[detailController release];
	[server release];
}

- (IBAction)toggleEdit {
    BOOL editing = !self.tableView.editing;
    self.navigationItem.rightBarButtonItem.enabled = !editing;
    self.navigationItem.leftBarButtonItem.title = (editing) ? @"Done" :  @"Edit";
    [self.tableView setEditing:editing animated:YES];
}

@end
